package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICourMaterialsManageService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 课程资料管理
 * @author  作者 E-mail: wufeng
 * @date 创建时间：2016年7月13日 下午12:11:28 
 * @version 1.0 
 * @parameter 
 * @since 
 * @return  
 */
@Controller
@RequestMapping("source")
public class CourMaterialsManageController {
	@Autowired
	private ICourMaterialsManageService courMaterialsManageService;
	/**
	* 获取课程资料列表
	* @author  作者 E-mail: wufeng
	* @date 创建时间：2016年7月14日 上午10:22:07 
	* @return  
	*/
	@RequestMapping("")
	public ModelAndView mv(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("courseMaking/material_list");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	@Login
	@RequestMapping("addMv")
	public ModelAndView addMv(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("courseMaking/file_add");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("type",1);
		return mv;
	}
	@RequestMapping("list/{type}")
	public void sourceList(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		DataVector<Map<String, Object>> listData = courMaterialsManageService.getSourceList(new RequestInput(request),type); 
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	/**
	* 上传     文件为获取
	* @author  作者 E-mail: wufeng
	* @date 创建时间：2016年7月14日 下午3:46:09 
	* @return  
	*/
	@RequestMapping("add")
	public void add(HttpServletRequest request,
			HttpServletResponse response){
		String json = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		Map<String, Object> map = JSONSerielizer.toMap(json);
		Map<String, Object> reMap = new HashMap<String, Object>();
		if (StringUtils.isEmpty(user)) {
			String errorInfo = "未登录！";
			OutputUtil.returnString(response,"未登录！");
		}else{
			reMap = courMaterialsManageService.add(map, user);
			String jsonString = JSONSerielizer.toJson(reMap, null, null);
			OutputUtil.returnString(response,jsonString);
		}
	}
	
	@RequestMapping("sourceForm/{id}")
	public ModelAndView sourceForm(@PathVariable String id){
		ModelAndView mv = new ModelAndView("courseMaking/course");
		mv.addObject("id",id);
		return mv;
		
	}
	/**
	* 获取课程资料内容(除文件)
	* @author  作者 E-mail: wufeng
	* @date 创建时间：2016年7月14日 上午10:56:10 
	* @return  
	*/
	@RequestMapping("sourceFormData/{id}")
	public void getSourceForm(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		boolean operate = true;//对资料的操作权限 true 可删除可修改
		Map<String, Object> map = new HashMap<String, Object>();
		/*if(courMaterialsManageService.isRight(id, user)){
			map = courMaterialsManageService.getMap(id);
		}else {
			operate = false;
		}*/
		map = courMaterialsManageService.getMap(id);
		map.put("operate", operate);
		String json = JSONSerielizer.toJson(map, null, null);
		OutputUtil.returnString(response, json);
		
	}
	/**
	* 上传者修改资料
	* @author  作者 E-mail: wufeng
	* @date 创建时间：2016年7月14日 下午3:22:50 
	* @return  
	*/
	@RequestMapping("update/{id}")
	public void update(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String id){
		String jsonMap = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey);
		boolean isSuccess = true; 
		String errorInfo = ""; 
		Map<String, Object> map = JSONSerielizer.toMap(jsonMap);
		Map<String, Object> remap = new HashMap<String, Object>();
		if(courMaterialsManageService.isRight(id, user)){
			isSuccess = courMaterialsManageService.update(id, map, user);
		}else{
			isSuccess = false;
			errorInfo = "没有权限修改";
		}
		remap.put("isSuccess", isSuccess);
		remap.put("errorInfo", errorInfo);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
	/**
	* 上传者删除资料（支持批量删除）
	* @author  作者 E-mail: wufeng
	* @date 创建时间：2016年7月14日 下午3:30:12 
	* @return  
	*/
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		boolean isSuccess = true;
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String str = request.getParameter("data");
		String []id = str.split(",");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean errorInfo = false;
		for (String string : id) {
			if (courMaterialsManageService.isRight(string, user)) {
				isSuccess = courMaterialsManageService.delete(string);
			}else { 
				errorInfo = true;
				isSuccess = false;
			}
		}
		map.put("isSuccess", isSuccess);
		map.put("errorInfo", errorInfo);
		String mapJson = JSONSerielizer.toJson(map, null, null); 
		OutputUtil.returnString(response, mapJson);
	}
	
	/*@RequestMapping("file")
	public ModelAndView fileMv(){
		ModelAndView mv = new ModelAndView("js/kindeditor/jsp/upload_json.jsp");
		return mv;
	}*/
}

